package com.baosight.risk.webservice.fltd.task;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.log.Logger;
import com.baosight.iplat4j.core.log.LoggerFactory;
import com.baosight.iplat4j.core.service.soa.XEventManager;
import com.baosight.risk.common.utils.DateUtils;
import com.baosight.risk.common.utils.EncryptUtil;
import org.apache.wink.client.ClientConfig;
import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;
import org.springframework.stereotype.Service;

import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class FLTDTaskBizImpl implements FLTDTaskBiz {

	private static Logger log = LoggerFactory.getLogger(FLTDTaskBiz.class);
	// 测试机 http://10.70.61.10:9081/bmc
//	private static final String MC_URL = "http://mc.baogang.info:9080/bmc/service/message/{msgNo}/subscriber/{empCode}";

	// 测试机 http://10.70.61.10:9081/bmc
//	private static final String MC_URL = "http://10.70.61.10:9081/bmc/service/message/{msgNo}/subscriber/{empCode}";

	// 正式机 http://mc.baogang.info/bmc
	private static final String MC_URL = "http://10.70.248.7:9080/bmc/service/message/{msgNo}/subscriber/{empCode}";

	/**
	 * 新增oa待办
	 *
	 */
	@Override
	public  void insertOA(String userCode, String msgNo, int num) {

//		String msgNo = "risk201912091234567892";
		if(msgNo!=null&&!"".equals(msgNo)){
			// 业务数据
			BSWMessage bean = new BSWMessage();
			bean.setMessageType("QMFX_TODO_BGTA");
			bean.setIdentification(userCode + DateUtils.format(new Date(), "yyyy-MM-ddHH:mm:ss"));
			bean.setSubject("宝钢股份全面风险管理平台您有" + num + "个待办待处理。");
			bean.setUrl("http://risk.baogang.info/risk/system/CasSso?userid="+ EncryptUtil.encrypt(userCode));
			bean.setFrom("admin");
			bean.setTo("U" + userCode);
			bean.setExpiryDate(DateUtils.plusDays(new Date(), 365));
			bean.setSendSysXbusCode("SPES");
			bean.setMsgNo(msgNo);
			// 业务数据组装结束....
			String eventId = "E_BG_MC_001";// E_DPBI01为项目组自定义 事件ID，前提是已关联发送消息服务S_EM_01

			EiInfo outInfo = new EiInfo();
			outInfo.set(EiConstant.eventId, eventId);
			String msg = BSWMessageUtils.getSendMsg(bean);

			outInfo.set("messageBody", msg);
			try {
				outInfo = XEventManager.call(outInfo);
				System.err.println(outInfo);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 关闭OA待办
	 *
	 */
	@Override
	public  void closeMessage(String empCode, String msgNo, int num) {

		ClientConfig clientConfig = new ClientConfig();
		clientConfig.connectTimeout(5000);
		clientConfig.readTimeout(5000);

//		String msgNo = "risk201911011234567890";

		String url = MC_URL.replace("{empCode}", empCode).replace("{msgNo}", msgNo);

		RestClient client = new RestClient(clientConfig);
		Resource resource = client.resource(url);
		resource.header("content-type", "application/json");
		resource.accept(MediaType.APPLICATION_JSON);
		ClientResponse restResponse = null;
		boolean isError = false;

		try {
			restResponse = resource.put("");
			System.out.println(restResponse);
			if (restResponse.getStatusCode() != 200) {
				log.error("禁用消息 调用失败,需要重发,返回码:" + restResponse.getStatusCode());
			}else{
                log.error("禁用消息 调用成功,返回码:" + restResponse.getMessage());
            }
		} catch (Exception e) {
			e.printStackTrace();
			isError = true;
			log.error("禁用消息 调用失败,需要重发:" + e.getMessage());
		}

//		// 加待办
//		if (!isError && num > 0) {
//			insertOA(empCode, num);
//		}

	}

	public static void main(String[] args) {
		String no = com.baosight.risk.common.utils.BSWMessageUtils.generateMsgNo();
        System.out.println(no);
	}

}
